package com.xh3140.xwidget.recycler;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

@SuppressWarnings("unused")
public class RecyclerSpaceDivider extends RecyclerView.ItemDecoration {
  private int _vertical;
  private int _horizontal;
  private int _left;
  private int _right;
  private int _top;
  private int _bottom;

  public RecyclerSpaceDivider(@Px int space) {
    _vertical = space;
    _horizontal = space;
  }

  public RecyclerSpaceDivider(@Px int verticalSpace, @Px int horizontalSpace) {
    _vertical = verticalSpace;
    _horizontal = horizontalSpace;
  }

  public RecyclerSpaceDivider(@Px int verticalSpace, @Px int horizontalSpace, @Px int padding) {
    _vertical = verticalSpace;
    _horizontal = horizontalSpace;
    _left = padding;
    _right = padding;
    _top = padding;
    _bottom = padding;
  }

  private void handleVerticalLayout(int position, @NonNull Rect outRect) {
    if (position == 0) {
      outRect.set(_left, _top, _right, _vertical);
    } else {
      outRect.set(_left, 0, _right, _vertical);
    }
  }

  private void handleHorizontalLayout(int position, @NonNull Rect outRect) {
    if (position == 0) {
      outRect.set(_left, _top, _horizontal, _bottom);
    } else {
      outRect.set(0, _top, _horizontal, _bottom);
    }
  }

  private void handleGridVerticalLayout(int span, int position, @NonNull Rect outRect) {
    if (position < span) {
      if (position == 0) {
        outRect.set(_left, _top, _horizontal, _vertical);
      } else if (position == span - 1) {
        outRect.set(0, _top, _right, _vertical);
      } else {
        outRect.set(0, _top, _horizontal, _vertical);
      }
    } else {
      if (position % span == 0) {
        outRect.set(_left, 0, _horizontal, _vertical);
      } else if (position % span == span - 1) {
        outRect.set(0, 0, _right, _vertical);
      } else {
        outRect.set(0, 0, _horizontal, _vertical);
      }
    }
  }

  private void handleGridHorizontalLayout(int span, int position, @NonNull Rect outRect) {
    if (position < span) {
      if (position == 0) {
        outRect.set(_left, _top, _horizontal, _vertical);
      } else if (position == span - 1) {
        outRect.set(_left, 0, _horizontal, _bottom);
      } else {
        outRect.set(_left, 0, _horizontal, _vertical);
      }
    } else {
      if (position % span == 0) {
        outRect.set(0, _top, _horizontal, _vertical);
      } else if (position % span == span - 1) {
        outRect.set(0, 0, _horizontal, _bottom);
      } else {
        outRect.set(0, 0, _horizontal, _vertical);
      }
    }
  }

  @Override
  public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    final RecyclerView.LayoutManager manager = parent.getLayoutManager();
    if (manager == null) {
      super.getItemOffsets(outRect, view, parent, state);
      return;
    }
    final int position = parent.getChildAdapterPosition(view);
    if (manager instanceof GridLayoutManager) {
      final int orientation = ((GridLayoutManager) manager).getOrientation();
      final int span = ((GridLayoutManager) manager).getSpanCount();
      if (span == 1) {
        if (orientation == RecyclerView.VERTICAL) {
          handleVerticalLayout(position, outRect);
        } else if (orientation == RecyclerView.HORIZONTAL) {
          handleHorizontalLayout(position, outRect);
        }
      } else {
        if (orientation == RecyclerView.VERTICAL) {
          handleGridVerticalLayout(span, position, outRect);
        } else if (orientation == RecyclerView.HORIZONTAL) {
          handleGridHorizontalLayout(span, position, outRect);
        }
      }
    } else if (manager instanceof LinearLayoutManager) {
      final int orientation = ((LinearLayoutManager) manager).getOrientation();
      if (orientation == RecyclerView.VERTICAL) {
        handleVerticalLayout(position, outRect);
      } else if (orientation == RecyclerView.HORIZONTAL) {
        handleHorizontalLayout(position, outRect);
      }
    }
  }

  @Px
  public int getLeftPadding() {
    return _left;
  }

  public RecyclerSpaceDivider setLeftPadding(@Px int left) {
    _left = left;
    return this;
  }

  @Px
  public int getRightPadding() {
    return _right;
  }

  public RecyclerSpaceDivider setRightPadding(@Px int right) {
    _right = right;
    return this;
  }

  @Px
  public int getTopPadding() {
    return _top;
  }

  public RecyclerSpaceDivider setTopPadding(@Px int top) {
    _top = top;
    return this;
  }

  @Px
  public int getBottomPadding() {
    return _bottom;
  }

  public RecyclerSpaceDivider setBottomPadding(@Px int bottom) {
    _bottom = bottom;
    return this;
  }

  @Px
  public int getVerticalSpace() {
    return _vertical;
  }

  public RecyclerSpaceDivider setVerticalSpace(@Px int verticalSpace) {
    _vertical = verticalSpace;
    return this;
  }

  @Px
  public int getHorizontalSpace() {
    return _horizontal;
  }

  public RecyclerSpaceDivider setHorizontalSpace(@Px int horizontalSpace) {
    _horizontal = horizontalSpace;
    return this;
  }

  public RecyclerSpaceDivider setPadding(@Px int padding) {
    _left = padding;
    _right = padding;
    _top = padding;
    _bottom = padding;
    return this;
  }

  public RecyclerSpaceDivider setPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
    _left = left;
    _right = right;
    _top = top;
    _bottom = bottom;
    return this;
  }
}
